package com.ms.oschina.ui.activity;

import android.content.Intent;

import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.model.LatLng;
import com.ms.oschina.R;
import com.ms.oschina.base.BaseActivity;
import com.ms.oschina.bean.Constants;
import com.ms.oschina.util.UiUtils;

public class MapActivity extends BaseActivity {

	private MapView mMapView;

	@Override
	protected int getLayoutId() {
		return R.layout.activity_map;
	}

	@Override
	protected void initView() {
		mMapView = (MapView) findViewById(R.id.mv_map);
	}

	@Override
	protected CharSequence getBackActionBarTitle() {
		return UiUtils.getString(R.string.actionbar_title_event_location);
	}

	@Override
	public void init() {
		// 注意该方法要再setContentView方法之前实现
		SDKInitializer.initialize(getApplicationContext());
	}

	@Override
	protected void initData() {
		BaiduMap map = mMapView.getMap();

		Intent intent = getIntent();

		String location = intent.getStringExtra(Constants.LOCATION);
		String[] position = location.split(",");
		intent.putExtra(Constants.PRECISION, position[0]);// 精度
		intent.putExtra(Constants.DIMENSIONALITY, position[1]);// 维度

		LatLng curPos = new LatLng(Double.parseDouble(position[1]),
				Double.parseDouble(position[0]));//

		MapStatusUpdate mapStatusUpdatePoint = MapStatusUpdateFactory
				.newLatLng(curPos);// 设置现在的位置
		map.setMapStatus(mapStatusUpdatePoint);
		map.setMaxAndMinZoomLevel(21, 13);
		//构建Marker图标  
		BitmapDescriptor bitmap = BitmapDescriptorFactory  
		    .fromResource(R.drawable.icon_marka);  
		//构建MarkerOption，用于在地图上添加Marker  
		OverlayOptions option = new MarkerOptions()  
		    .position(curPos)  
		    .icon(bitmap);  
		//在地图上添加Marker，并显示  
		map.addOverlay(option);
		// 改变当前地图的状态
		MapStatusUpdate mapStatusUpdate = MapStatusUpdateFactory.zoomTo(20);// 设置缩放到15级，默认是12级
		map.setMapStatus(mapStatusUpdate);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		mMapView.onDestroy();
	}

	@Override
	protected void onResume() {
		super.onResume();
		mMapView.onResume();
	}

	@Override
	protected void onPause() {
		super.onPause();
		mMapView.onPause();
	}
}
